using System;
using System.Collections.Generic;
using System.Text;

using HandyIRCCore;

namespace HandyIRCChat
{
    public class PrivateObject
    {
        public PrivateObject(HandyLogic hl, Core c, string name, string firstMessage)
        {
            handyLogic = hl;
            ircCore = c;
            userNick = name;

            handyLogic.QueryMessageEvent += 
                new HandyLogic.QueryMessageEventHandler(handyLogic_QueryMessageEvent);
            ircCore.NickEvent += new Core.NickEventHandler(ircCore_NickEvent);

            if (firstMessage != null)
            {
                string message = "<" + name + "> " + firstMessage + Environment.NewLine;
                AppendToMessageBuffer(message);

                if (PrivateMessageEvent != null)
                    PrivateMessageEvent(this, message);
            }
        }

        #region Declarations

        private const int bufferMaxLength = 4096;

        // Length cannot be greater than bufferMaxLength
        private string messageBuffer = "";

        private HandyLogic handyLogic;

        private Core ircCore;

        private string userNick;

        private PrivateForm privateForm = null;

        public event PrivateMessageEventHandler PrivateMessageEvent;

        public delegate void PrivateMessageEventHandler(object sender,
                                                        string message);

        public event ShowPrivateFormEventHandler ShowPrivateFormEvent;

        public delegate void ShowPrivateFormEventHandler(object sender,
                                                         string userName);

        #endregion

        #region Properties

        public string MessageBuffer
        {
            get
            {
                return messageBuffer;
            }
        }

        public string UserName
        {
            get
            {
                return userNick;
            }
        }

        public PrivateForm PrivateForm
        {
            get
            {
                return privateForm;
            }

            set
            {
                privateForm = value;
            }
        }

        #endregion

        #region Methods

        public void SendMessage(string s)
        {
            if (handyLogic.IsConnected)
            {
                ircCore.SendMessage("PRIVMSG " + userNick + " " + s);

                string message = "<" + ircCore.UserNick + "> " + s + Environment.NewLine;
                AppendToMessageBuffer(message);

                if (PrivateMessageEvent != null)
                    PrivateMessageEvent(this, message);
            }
        }

        private void handyLogic_QueryMessageEvent(object sender, HandyIRCMessage msg)
        {
            if (msg.Nick == userNick)
            {
                string message = "<" + msg.Nick + "> " + msg.Parameters[1] + Environment.NewLine;
                AppendToMessageBuffer(message);

                if (PrivateMessageEvent != null)
                    PrivateMessageEvent(this, message);

                if (privateForm == null)
                {
                    if (ShowPrivateFormEvent != null)
                        ShowPrivateFormEvent(this, userNick);
                }
            }
        }

        private void ircCore_NickEvent(object sender, HandyIRCMessage msg)
        {
            if (msg.Nick == userNick)
                userNick = msg.Parameters[0];
        }

        private void AppendToMessageBuffer(string message)
        {
            StringBuilder sb = new StringBuilder(messageBuffer);

            sb.Append(message);
            if (sb.Length > bufferMaxLength)
                sb.Remove(0, sb.Length - bufferMaxLength);

            messageBuffer = sb.ToString();
        }

        #endregion
    }
}
